package henu.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import henu.bean.Teacher;

import henu.dao.ITeacherDao;
import henu.factory.DaoFactory;
import henu.util.Dhcp;


/**
 * @author 纪柳任
 * 2016年9月19日下午9:42:24
 */
public class TeacherDaoImpl implements ITeacherDao{


	protected QueryRunner queryRunner;
    private int rows = 0;
	public TeacherDaoImpl(QueryRunner queryRunner) {
		super();
		this.queryRunner = queryRunner;
	}
	@Override
	public int add(Teacher teacher) {
		queryRunner =  new QueryRunner(Dhcp.getDataSource());
		String sql = "insert into tb_teacher(fTeacherId,fTeachName,fJobTitle,fDept,fPhoneNumber,fEmail,fQQ,fMemo) values(?,?,?,?,?,?,?,?)";
		Object[] param ={teacher.getfTeacherId(),teacher.getfTeachName(),teacher.getfJobTitle(),teacher.getfDept(),teacher.getfPhoneNumber(),teacher.getfEmail(),teacher.getfQQ(),teacher.getfMemo()};
		try {
			rows=queryRunner.update(sql, param);
        System.out.println(rows+"行已经发生变化");
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return rows;
	}

	@Override 
	public int delete(String fteacherId) {
		// TODO Auto-generated method stub
	
		String sql="delete from tb_teacher where fTeacherId=?";
		Object[] param={fteacherId};
		try {
			rows=queryRunner.update(sql,param);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rows;
	}

	@Override
	public Teacher get(String fteacherId) {
		String sql = "select * from tb_teacher where fTeacherId=?";
		Object param[]={fteacherId};
		Teacher teacher = null;
		try {
			teacher = queryRunner.query(sql, param,new BeanHandler<Teacher>(Teacher.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return teacher;
	
	}
	
	public List<Teacher> getById(String fteacherId){
		String sql = "select * from tb_teacher where fTeacherId=?";
		Object param[]={fteacherId};
		List<Teacher> teachers = null;
		try {
			teachers = queryRunner.query(sql,param, new BeanListHandler<Teacher>(Teacher.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return teachers;
	}
	
	public List<Teacher> getByName(String teachername) {
		String sql = "select * from tb_teacher where fTeachName=?";
		Object param[]={teachername};
		List<Teacher> teachers = null;		
		try {
			teachers = queryRunner.query(sql,param, new BeanListHandler<Teacher>(Teacher.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return teachers;
	
	}
	
	public List<Teacher> getByDept(String dept){
		String sql = "select * from tb_teacher where fDept=?";
		Object param[]={dept};
		List<Teacher> teachers = null;		
		try {
			teachers = queryRunner.query(sql,param, new BeanListHandler<Teacher>(Teacher.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return teachers;
	}
	

	@Override
	public int update(Teacher teacher) {
		// TODO Auto-generated method stub
		String sql="update tb_teacher set fDept=?,fPhoneNumber=?,fEmail=?,fQQ=?,fMemo=? where fJobTitle=?";
		Object param[]={teacher.getfDept(),teacher.getfPhoneNumber(),teacher.getfEmail(),teacher.getfQQ(),teacher.getfMemo(),teacher.getfJobTitle()};
		int n = 0;
		try{
			
			n=queryRunner.update(sql,param);
			
		}
		catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
	    return n;
	}

	@Override
	public List<Teacher> getAll() {
		queryRunner =  new QueryRunner(Dhcp.getDataSource());
		String sql = "select * from tb_teacher";
		List<Teacher> teachers = null;		
		try {
			teachers = queryRunner.query(sql, new BeanListHandler<Teacher>(Teacher.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return teachers;
	}
	@Override
	public List<Teacher> getAll(int pageSize, int pageNow) {
		queryRunner =  new QueryRunner(Dhcp.getDataSource());
		String sql = "select * from tb_teacher limit "+(pageNow*pageSize-pageSize)+","+pageSize;
		List<Teacher> teachers = null;		
		try {
			teachers = queryRunner.query(sql, new BeanListHandler<Teacher>(Teacher.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return teachers;
	}
	@Override
	public int getCount() {
		queryRunner =  new QueryRunner(Dhcp.getDataSource());
		String sql = "select * from tb_teacher";
		List<Teacher> teachers = null;		
		try {
			teachers = queryRunner.query(sql, new BeanListHandler<Teacher>(Teacher.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return teachers.size();
	}
	
	
}
